package com.api.browser.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.api.browser.service.BrowserService;

import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * 流程信息查询
 * INSERT INTO wf_browser_config (type, clazz, description, linkurl, titelebel, icon, iconColor, iconBgcolor, mobileLinkUrl) VALUES
 * ('288', 'com.api.browser.service.impl.WorkflowRequestBrowserService', '流程信息查询', 'NULL', NULL, NULL, NULL, NULL, NULL)
 * 拷贝 class 到 %weaver%\ecology\classbean\com\api\browser\service\impl
 */
public class WorkflowRequestBrowserService extends BrowserService {

	@Override
	@SuppressWarnings("deprecation")
	public Map<String, Object> getBrowserData(Map<String, Object> paramMap) throws Exception {
		HashMap<String, Object> hashMap = new HashMap<>();
		String formid = Util.null2String(paramMap.get("formid"));
		String requestid = Util.null2String(paramMap.get("requestid"));
		String all = Util.null2String(paramMap.get("all"));
		String select = "id";
		if (all.length() > 0 && "1".equals(all)) {
			select = "*";
		}
		//
		HashMap<Object, Object> dataMap = new HashMap<>();
		// main data
		HashMap<String, Object> mainMap = new HashMap<>();
		String sql = "select " + select + " from formtable_main_" + formid + " where requestid = " + requestid;
		RecordSet recordSet = new RecordSet();
		recordSet.executeSql(sql);
		while (recordSet.next()) {
			String[] columns = recordSet.getColumnName();
			for (String column : columns) {
				mainMap.put(column, recordSet.getString(column));
			}
		}
		dataMap.put("mainData", mainMap);
		// detail data
		ArrayList<HashMap<String, Object>> detailList = new ArrayList<>();
		sql = "select d." + select + " from formtable_main_" + formid + " m, formtable_main_" + formid + "_dt1 d where m.id = d.mainid and m.requestid = " + requestid;
		recordSet = new RecordSet();
		recordSet.executeSql(sql);
		while (recordSet.next()) {
			HashMap<String, Object> detailMap = new HashMap<>();
			String[] columns = recordSet.getColumnName();
			for (String column : columns) {
				detailMap.put(column, recordSet.getString(column));
			}
			detailList.add(detailMap);
		}
		dataMap.put("detailData", detailList);
		// 返回
		ArrayList<HashMap<Object, Object>> datalList = new ArrayList<>();
		datalList.add(dataMap);
		hashMap.put("datas", datalList);
		return hashMap;
	}

	@Override
	@SuppressWarnings("deprecation")
	public Map<String, Object> browserAutoComplete(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse) throws Exception {
		HashMap<String, Object> hashMap = new HashMap<>();
		String formid = Util.null2String(paramHttpServletRequest.getParameter("formid"));
		String requestid = Util.null2String(paramHttpServletRequest.getParameter("requestid"));
		String all = Util.null2String(paramHttpServletRequest.getParameter("all"));
		String select = "id";
		if (all.length() > 0 && "1".equals(all)) {
			select = "*";
		}
		//
		HashMap<Object, Object> dataMap = new HashMap<>();
		// main data
		HashMap<Object, Object> mainMap = new HashMap<>();
		String sql = "select " + select + " from formtable_main_" + formid + " where requestid = " + requestid;
		RecordSet recordSet = new RecordSet();
		recordSet.executeSql(sql);
		while (recordSet.next()) {
			String[] columns = recordSet.getColumnName();
			for (String column : columns) {
				mainMap.put(column, recordSet.getString(column));
			}
		}
		dataMap.put("mainData", mainMap);
		// detail data
		ArrayList<HashMap<Object, Object>> detailList = new ArrayList<>();
		sql = "select d." + select + " from formtable_main_" + formid + " m, formtable_main_" + formid + "_dt1 d where m.id = d.mainid and m.requestid = " + requestid;
		recordSet = new RecordSet();
		recordSet.executeSql(sql);
		while (recordSet.next()) {
			HashMap<Object, Object> detailMap = new HashMap<>();
			String[] columns = recordSet.getColumnName();
			for (String column : columns) {
				detailMap.put(column, recordSet.getString(column));
			}
			detailList.add(detailMap);
		}
		dataMap.put("detailData", detailList);
		// 返回
		ArrayList<HashMap<Object, Object>> datalList = new ArrayList<>();
		datalList.add(dataMap);
		hashMap.put("datas", datalList);
		return hashMap;
	}
}
